<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
	UserBean user = (UserBean)request.getSession().getAttribute("userBean");
	String _db = user.getDbType();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<base href="<%=basePath%>"/>

		<title>角色</title>
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache"/>
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0"/>
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
		<link type="text/css" rel="stylesheet" href="ligerUI/skins/ligerui-icons.css" />
		<link type="text/css" rel="stylesheet" href="resource/css/style.css" />

		<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
		<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
		<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script type="text/javascript" src="js/validata.js"></script>

		<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
		<script src="ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
		<script type="text/javascript">
			var _db = '<%=_db%>';
			
			_jsonObj.classname = "com.wxtf.platform.plugins.role.RoleAction";
			_jsonObj.methodname = "queryPage";
			_jsonObj.extenddata = '';
			var manager;//角色表格对象
			var roleManager;//已选用户表格对象
			var userManager;//可选用户表格对象
			var roleid;//角色id
			var groupId;
			var menuId;
			var userparam;//可选用户定义的参数
			var roleparam;//已选用户定义的参数			
	        $(function(){
	        	//定义角色表格	        	
				manager = $("#role-grid").ligerGrid({
					url: _url, parms: _jsonObj,
					dataAction:'server',method:'post',enabledSort:true,
	                columns: [
	                    { display: 'role_id', name: 'role_id', hide: 1 ,width: 1},	                    
	                    { display: 'rparent_id', name: 'rparent_id', hide: 1 ,width: 1},
	                	{ display: '角色名称',isSort:true,sortnameParmName:'caption', name: 'caption', width: 300, align: 'left'},
	                	{ display: '排序号',isSort:true,sortnameParmName:'logic_code', name: 'logic_code',width: 100, align: 'left' },
	                	{ display: 'action', name: 'action',align: 'left', hide: 1 ,width: 1},
	                	{ display: 'action1', name: 'action1', hide: 1 ,width: 1},
	                    { display: 'action2', name: 'action2', hide: 1 ,width: 1},
	                	{ display: '编辑', isSort:false,name:'edit',width:100,render:function(record, index, value, oth){
	                			if(record.role_id!="0"){
	                				return '<a href="javascript:editRole(\'' + record.role_id + '\',\'' + record.rparent_id + '\',\''+record.caption+'\',\'' + record.logic_code + '\',\'' + record.action + '\',\'' + record.action1 + '\',\'' + record.action2 + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/edit.gif"/></div>';
	                			}	                			
	                		}
	                	},
	                	{ display: '选择用户',isSort:false, name:'edit',width:100,render:function(record, index, value, oth){
	                			return '<a href="javascript:viewUser(\'' + record.role_id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/memeber.gif"/></div>';
	                		}
	                	},
	                	{ display: '选择菜单',isSort:false, name:'menu1',width:100,render:function(record, index, value, oth){
	                			return '<a href="javascript:viewMenu(\'' + record.role_id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/archives.gif"/></div>';
	                		}
	                	},
	                	{ display: '选择用户分组',isSort:false, name:'group',width:100,render:function(record, index, value, oth){
	                			return '<a href="javascript:viewGroup(\'' + record.role_id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/msn.gif"/></div>';
	                		}
	                	}
	                ],
	                onClickRows: function (data, rowindex, rowobj){
	                	
                	},
	                width: '100%', height:'100%',heightDiff: 2,usePager:true,rownumbers:true,checkbox:true,
	                alternatingRow: true,
	                toolbar: { items: [
                	{ text: '新建', click: addRole, icon: 'add' },
                	{ line: true },
                	{ text: '删除', click: delRole, icon: 'delete' }
                	]
                	}
	            });
	        });	        
	        /*
	         * Grid页面刷新
	         */
	        function reload(){
	        	_jsonObj.methodname = "queryPage";
				_jsonObj.extenddata = '';
				_jsonObj.logicdata = '';
				manager.loadData();
	        }
	        var win;
        	//打开窗口	        
	        function f_openWindow(target, title, width, height, func) {
				if(win==null){
	            	win = initWindowByDiv2(target, title, width, height, func);
				}else{
					win.show();
				}
        	}
        	var win1;
        	//打开窗口	        
	        function f_openWindow1(target, title, width, height, func, func2) {
				if(win1==null){
	            	win1 = initWindowByDiv(target, title, width, height, func, func2);
				}else{
					win1.show();
				}
        	}
        	var win2;
        	//打开窗口	        
	        function f_openWindow2(target, title, width, height, func, func2) {
				if(win2==null){
	            	win2 = initWindowByDiv(target, title, width, height, func, func2);
				}else{
					win2.show();
				}
        	}
        	var win3;
        	//打开窗口	        
	        function f_openWindow3(target, title, width, height, func, func2) {
				if(win3==null){
	            	win3 = initWindowByDiv(target, title, width, height, func, func2);
				}else{
					win3.show();
				}
        	}
        	var win4;
        	//打开窗口	        
	        function f_openWindow4(target, title, width, height, func, func2) {
				if(win4==null){
	            	win4 = initWindowByDiv(target, title, width, height, func, func2);
				}else{
					win4.show();
				}
        	}
        	//角色选择用户
			function save_user(){
				//获取可选用户表格以选中的复选框
				var save_rows = userManager.getCheckedRows();
	        	var str = "";
	        	//循环取出user_id
	        	$(save_rows).each(function (){
	        	    str += this.user_id + ",";
	        	});
	        	//不加最后的逗号
	            str=str.substring(0,str.length-1);
	            if(str==""){
	            	$.ligerDialog.warn('请选择要添加的用户!');
	            	return;
	            }	                
	             //定义保存的参数
	             var param = {
					"classname": "com.wxtf.platform.plugins.role.RoleAction",
					"methodname": "saveUser",
					"logicdata": [],
					"extenddata": []
				};
			     param.logicdata = "[{\"roleId\":\""+roleid+"\",\"userId\":\""+str+"\"}]";
			     callserver(param, function(data, satus){
					succMsgTip(data.resultrecord);
					//刷新已选用户的表格
	            	roleManager.loadData();
					//刷新可选用户的表格
	            	userManager.loadData();
				},function(data, satus){
					warningTip(data.resultrecord);
				})
			}
			//角色移除用户
			function del_user(){
				//获取已选用户表格以选中的复选框
				var save_rows = roleManager.getCheckedRows();
	        	var str = "";
	        	//循环取出user_id
	        	 $(save_rows).each(function (){
	        	     str += this.user_id + ",";
	        	 });
	        	 //不加最后的逗号
	             str=str.substring(0,str.length-1);
	             if(str==""){
	            	$.ligerDialog.warn("请选择要移除的用户!");
	            	return;
	            }
	             //定义删除的参数
	             var param = {
					"classname": "com.wxtf.platform.plugins.role.RoleAction",
					"methodname": "delUser",
					"logicdata": [],
					"extenddata": []
				};
			     param.logicdata = "[{\"roleId\":\""+roleid+"\",\"userId\":\""+str+"\"}]";
			     callserver(param, function(data, satus){
					succMsgTip(data.resultrecord);
					//刷新已选用户的表格
	            	roleManager.loadData();
					//刷新可选用户的表格
	            	userManager.loadData();
				},function(data, satus){
					warningTip(data.resultrecord);
				})
			}
			//已选用户查询
			function query_role_user(){
				//获取查询条件
				var userName = $('#role_query').val();
				var jigou = $('#role_query_jigou').val();
				//刷新已选用户表格
				roleparam.extenddata = "[{\"roleId\":\""+roleid+"\",\"userName\":\""+userName+"\",\"jigou\":\""+jigou+"\"}]";
				roleManager.loadData();
			}
			//可选用户查询
			function query_user(){
				//获取查询条件
				var userName = $('#user_query').val();
				var jigou = $('#user_query_jigou').val();
				//刷新可选用户表格
				userparam.logicdata = '[{\"roleId\":\"'+roleid+'\",\"userName\":\"'+userName+'\",\"jigou\":\"'+jigou+'\"}]';
				userManager.loadData();
			}
			//打开角色选择用户窗口
			function viewUser(val){
				//将角色id赋值给全局变量roleid
				roleid=val;
				
				f_openWindow('listUser', '选择用户',810, 460,function(){
					win.hide(); 
				});
				
				//设置已选用户参数				
				roleparam = {
					"classname": "com.wxtf.platform.plugins.role.RoleAction",
					"methodname": "listUser4Role",
					"logicdata": [],
					"extenddata": []
				};
				roleparam.extenddata = '[{\"roleId\":\"'+val+'\",\"userName\":\"\",\"jigou\":\"\"}]';
				//定义已选用户表格
	            roleManager = $("#role_div").ligerGrid({
	            	url: _url, 
	            	parms: roleparam,
	            	async:false,
	            	enabledSort:false,
					dataAction:'server',
					method:'post',
					width: '360', height:'325',
					checkbox: true,
	                usePager:true,
	                alternatingRow: true,//奇偶行显示效果
	                rownumbers:true,
	                columns: [
	                    { display: '用户ID',  name: 'user_id',align: 'left',hide: 1 ,width: 1},
	                	{ display: '已选用户', name: 'caption',align: 'left' ,width: 200}
	                ]
            	});
            	//设置可选用户参数	
            	userparam = {
					"classname": "com.wxtf.platform.plugins.role.RoleAction",
					"methodname": "listUser4",
					"logicdata": [],
					"extenddata": []
				};
				userparam.logicdata = '[{"roleId":"'+val+'","userName":"","jigou":""}]';
				//定义可选用户表格
	            userManager = $("#user_div").ligerGrid({
	            	url: _url, 
	            	parms: userparam,
	            	async:false,
					dataAction:'server',
					method:'post',
					width: '360', height:'325',
					checkbox: true,
	                usePager:true,
	                enabledSort:false,
	                alternatingRow: true,//奇偶行显示效果
	                rownumbers:true,
	                columns: [
	                    { display: '用户ID', name: 'user_id', align: 'left', hide: 1 ,width: 1},
	                	{ display: '可选用户', name: 'caption', align: 'left' ,width: 200}
	                ]	                
            	});
			}
        	//新建角色	        
	        function addRole()
        	{
        		document.forms[0].reset();	
				f_openWindow1('insertRole', '新建窗口', 350, 150,function(){	
						var erparent_id = $("#rparent_id").val();
						var ecaption = $("#caption").val();
						var elogic_code = $("#logic_code").val();
						var eaction = $("#action").val();
						var eaction1 = $("#action1").val();
						var eaction2 = $("#action2").val();
						if(fucCheckLength(ecaption.trim()) <= 0 || fucCheckLength(ecaption.trim()) > 50){
							warningTip("角色名称：不能为空且不能超过25字符");
							return;
						}
						if(fucCheckLength(elogic_code.trim()) <= 0){
							warningTip("排序号：不能为空");
							return;
						}
						if(!checkIsNum(elogic_code.trim())){
							warningTip("排序号：必须为整数");
							return;
						}
						if(Math.abs(parseInt(elogic_code)) > 1000000){
							warningTip("排序号：不能超过1000000");
							return;
						}
						var param = {
							"classname": "com.wxtf.platform.plugins.role.RoleAction",
							"methodname": "save",
							"logicdata": [],
							"extenddata": []
						};
						param.logicdata = '[{"role_id":"", "rparent_id":"'+erparent_id+'", "caption":"'+ecaption+'", "logic_code":"'+elogic_code+'", "action":"'+eaction+'", "action1":"'+eaction1+'", "action2":"'+eaction2+'"}]';
						callserver(param, function(data, satus){
						$("#insertRoleForm input,.l-textarea").ligerHideTip();
						if(data.result){
							win1.hide();
						}
						succMsgTip(data.resultrecord);
						reload();
						},function(data, satus){
							warningTip(data.resultrecord);
						});
				}, function(){
					$("#insertRoleForm input,.l-textarea").ligerHideTip();
					win1.hide();
				});       		
        	}      	
        	//编辑角色
        	function editRole(r_role_id,r_rparent_id,r_caption,r_logic_code,r_action,r_action1,r_action2)
        	{
        		document.forms[1].reset();
				$("#erole_id").val(r_role_id);
				$("#erparent_id").val(r_rparent_id);
				$("#ecaption").val(r_caption);
				$("#ecaption1").val(r_caption);
				$("#elogic_code").val(r_logic_code);
				$("#eaction").val(r_action);
				$("#eaction1").val(r_action1);
				$("#eaction2").val(r_action2);
				f_openWindow2('updateRole', '编辑窗口', 350, 150,function(){
						var erole_id = $("#erole_id").val();						
						var erparent_id = $("#erparent_id").val();
						var ecaption = $("#ecaption").val();
						var ecaption1 = $("#ecaption1").val();
						var elogic_code = $("#elogic_code").val();
						var eaction = $("#eaction").val();
						var eaction1 = $("#eaction1").val();
						var eaction2 = $("#eaction2").val();
						if(fucCheckLength(ecaption.trim()) <= 0 || fucCheckLength(ecaption.trim()) > 50){
							warningTip("角色名称：不能为空且不能超过25字符");
							return;
						}
						if(fucCheckLength(elogic_code.trim()) <= 0){
							warningTip("排序号：不能为空");
							return;
						}
						if(!checkIsNum(elogic_code.trim())){
							warningTip("排序号：必须为整数");
							return;
						}
						if(Math.abs(parseInt(elogic_code)) > 1000000){
							warningTip("排序号：不能超过1000000");
							return;
						}
						var param = {
							"classname": "com.wxtf.platform.plugins.role.RoleAction",
							"methodname": "update",
							"logicdata": [],
							"extenddata": []
						};
						param.logicdata = '[{"role_id":"'+erole_id+'", "rparent_id":"'+erparent_id+'", "caption":"'+ecaption+'","caption1":"'+ecaption1+'", "logic_code":"'+elogic_code+'", "action":"'+eaction+'", "action1":"'+eaction1+'", "action2":"'+eaction2+'"}]';
						callserver(param, function(data, satus){
						$("#updateRoleForm input,.l-textarea").ligerHideTip();
						if(data.result){
							win2.hide();
						}
						succMsgTip(data.resultrecord);
						reload();
						},function(data, satus){
							warningTip(data.resultrecord);
						});
				}, function(){
					$("#updateRoleForm input,.l-textarea").ligerHideTip();
					win2.hide();
				});        		
        	}
        	//删除角色
        	function delRole()
        	{
        		//定义删除角色参数
        		var param = {
					"classname": "com.wxtf.platform.plugins.role.RoleAction",
					"methodname": "delete",
					"logicdata": [],
					"extenddata": []
				};
        		var str = "";
        		var s = "";        		
        		//获取角色表格以选中的复选框
        		var rows = manager.getCheckedRows();
        		$(rows).each(function (){
        	    	str += this.role_id + ",";
        	    	if(this.role_id=="0"){
        	    		s+=this.caption+","
        	    	}
        		});
        		s=s.substring(0,s.length-1);
        		if(s!=""){
        			$.ligerDialog.warn(s+"是系统角色，不可删除");
					return;
        		}
        		str=str.substring(0,str.length-1);
        		if(str==""){
					$.ligerDialog.warn("没有选中项");
					return;
				}
        		$.ligerDialog.confirm('确认删除角色的操作吗?', function (ml) { 			
					if(ml==true){
		       			if(str!=""){
							param.logicdata = '[{"ids":"'+str+'"}]';
  							callserver(param,function(data, satus){
								succMsgTip(data.resultrecord);
								reload();
							},function(data, satus){
								warningTip(data.resultrecord);
							});
						}
					}
				});       		  				
        	}
			function query(){
				
			}
			function getData(){
				
			}
			//选择分组
			function viewGroup(roleId){				
				groupId = roleId;
				//定义角色选择分组的参数
				var param = {
					"classname": "com.wxtf.platform.plugins.group.GroupAction",
					"methodname": "listGroup4Role",
					"logicdata": [],
					"extenddata": []
				};
				param.logicdata = '[{"roleId":"' + groupId + '","groupName":""}]';
				//定义分组tree
	            var groupTree = $("#groupDiv").ligerTree({ checkbox: true,nodeWidth: 250 });
	            //清空分组tree
	            groupTree.clear();            
	            //加载分组tree
	            groupTree.loadData('', _url, param);
	            f_openWindow4('groupDiv', '选择用户分组', 400, 350, function(){
	            	var ids = '';
	            	var obj = groupTree.getCheckeds(); 
	            	for(var i=0; i<obj.length; i++){
	            		ids += obj[i].data.id + ',';
	            	}
	            	if(ids != ''){
	            		ids = ids.substr(0, ids.length - 1);
	            	}
        			param.classname = "com.wxtf.platform.plugins.role.RoleAction";
					param.methodname = "saveGroup";
					param.logicdata = '[{"roleId":"' + groupId + '","groupId":"' + ids + '"}]';
        			callserver(param,function(data, satus){
						succMsgTip(data.resultrecord);
						win4.hide();
					},function(data, satus){
						warningTip(data.resultrecord);
					});
        		},function(){
        			win4.hide();
        		});
			}
			
			/*
			 * 角色选菜单
			 */
			function viewMenu(roleId){
				menuId = roleId;
				//定义角色选择菜单的参数
				var param = {
					"classname": "com.wxtf.platform.plugins.permissions.PermissionsAction",
					"methodname": "menuTree",
					"logicdata": [],
					"extenddata": []
				};
				param.logicdata = '[{"roleId":"' + menuId + '"}]';
				//定义菜单tree
	            var menuTree = $("#menuDiv").ligerTree({ checkbox: true,nodeWidth: 250 });
	            //清空菜单tree
	            menuTree.clear();
	            //加载菜单tree
	            menuTree.loadData('', _url, param);
	            
	            f_openWindow3('menuDiv', '选择菜单', 400, 350, function(){
	            	var ids = '';
	            	var obj = menuTree.getCheckeds(); 
	            	for(var i=0; i<obj.length; i++){
	            		ids += obj[i].data.id + ',';
	            	}
	            	if(ids == ''){
	            		ids = '';
	            	} else {
	            		ids = ids.substr(0, ids.length - 1);
	            	}
	            	
        			param.classname = "com.wxtf.platform.plugins.role.RoleAction";
					param.methodname = "saveMenuTree";
					param.logicdata = '[{"roleId":"' + menuId + '","permissionId":"' + ids + '"}]';
        			callserver(param,function(data, satus){
						succMsgTip(data.resultrecord);
						win3.hide();
					},function(data, satus){
						warningTip(data.resultrecord);
					});
        		},function(){
        			win3.hide();
        		});
	            
			}
			
		</script>
	</head>

	<body style="padding-top: 4px;">
		<div id="role-grid"></div>
		<div id="listUser" style="display: none;">
			<table align="center" class="form_table">
				<tr>
					<td>
						<div align="left">
							用户：<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" id="role_query"></input><br/>
							机构：<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" id="role_query_jigou"></input>
						 	<input class="btn1" type="button" value="查询" onclick="query_role_user()"/>
						</div>
						<div id="role_div"></div>
					</td>
					<td align="center">
						<input class="btn1" type="button" value="选择" onclick="save_user()"/>
						<br/> <br/> <br/> <br/> <br/><br/> <br/> <br/> <br/> <br/>
						<input class="btn1" type="button" value="移除 " onclick="del_user()"/>
					</td>
					<td>
						<div align="left">
							用户：<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" id="user_query"></input><br/>
						    机构：<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" id="user_query_jigou"></input><input class="btn1" type="button" value="查询" onclick="query_user()"/> 	
						</div>
						<div id="user_div"></div>
					</td>
				</tr>
			</table>
		</div>
		<div id="insertRole" style="display: none;">
			<form id="insertRoleForm">
				<input type="hidden" id="rparent_id" name="rparent_id" value="0"/>				
				<input type="hidden" id="action" name="action" value="0" />
				<input type="hidden" id="action" name="action" value="0" />
				<input type="hidden" id="action1" name="action1" value="0" />
				<input type="hidden" id="action2" name="action2" value="0" />
				<table class="form_table">
					<tr>
						<td class="form_label">
							角色名：
						</td>
						<td>
							<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" type="text" id="caption" name="caption"/>
						</td>
						<td align="left" style="color: red; ">*</td>
					</tr>					
					<tr>
						<td class="form_label">
							排序号：
						</td>
						<td>
							<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" type="text" id="logic_code" name="logic_code"/>
						</td>
						<td align="left" style="color: red; ">*</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="updateRole" style="display: none;">
			<form id="updateRoleForm">
				<input type="hidden" id="erole_id" name="erole_id" value="" />
				<input type="hidden" id="ecaption1" name="ecaption1" value=""/>
				<input type="hidden" id="erparent_id" name="erparent_id" value="0"/>
				<input type="hidden" id="eaction" name="eaction" value="0" />
				<input type="hidden" id="eaction1" name="eaction1" value="0" />
				<input type="hidden" id="eaction2" name="eaction2" value="0" />
				<table class="form_table">
					<tr>
						<td class="form_label">
							角色名：
						</td>
						<td>
							<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" type="text" id="ecaption" name="ecaption"/>
						</td>
						<td align="left" style="color: red; ">*</td>
					</tr>					
					<tr>
						<td class="form_label">
							排序号：
						</td>
						<td>
							<input class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'" type="text" id="elogic_code" name="elogic_code"/>
						</td>
						<td align="left" style="color: red; ">*</td>
					</tr>
				</table>
			</form>
		</div>
		<!-- 角色选菜单 -->
		<ul id="menuDiv">
		</ul>
		<!-- 角色选分组 -->
		<ul id="groupDiv">
		</ul>
	</body>
</html>
